﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using NHibernate.Mapping;

namespace NHibernate.Audit.Cfg
{
    public abstract class RevisionConfiguration
    {

        private PropertyInfo revisionIdentityProperty;
        private System.Type revisionIdentityPropertyType;

        /// <summary>
        /// Name for audit table's revision column
        /// </summary>
        /// <returns></returns>
        public virtual string GetRevisionColumnName(Table table)
        {
            return "Revision";
        }

        /// <summary>
        /// Name for audit entity's revision property
        /// </summary>
        /// <returns></returns>
        public virtual string GetRevisionPropertyName(PersistentClass Class)
        {
            return GetRevisionPropertyName(Class.EntityName);
        }

        public virtual string GetRevisionPropertyName(string entityName)
        {
            return "Revision";
        }

        /// <summary>
        /// Type for the revision entity
        /// </summary>
        /// <returns></returns>
        public abstract System.Type GetRevisionClass();

        public abstract string RevisionEntityName { get; set; }

        /// <summary>
        /// Id property of the revision entity
        /// </summary>
        public abstract string GetRevisionIdentityPropertyName();

        /// <summary>
        /// Timestamp property of the revision entity
        /// </summary>
        public abstract string GetRevisionTimestampPropertyName();



        /// <summary>
        /// Id property of the revision entity
        /// </summary>
        /// <returns></returns>
        public virtual PropertyInfo GetRevisionIdentityProperty()
        {
            if (null == revisionIdentityProperty)
            {
                var revisionClass = GetRevisionClass();
                var revisionIdName = GetRevisionIdentityPropertyName();
                revisionIdentityProperty = revisionClass.GetProperty(revisionIdName);
            }
            return revisionIdentityProperty;
        }

        public virtual System.Type GetRevisionIdentityPropertyType()
        {
            if (null == revisionIdentityPropertyType)
            {
                revisionIdentityPropertyType = GetRevisionIdentityProperty().PropertyType;
            }
            return revisionIdentityPropertyType;
        }


    }
}
